#!/bin/bash 
#
# 安装完成后，会将一些账号密码信息，如数据库的，保存到 /root/account.txt 中
#

# 加载资源文件下载路径
cd ~
wget https://gitee.com/HuaweiCloudDeveloper/huaweicloud-solution-build-wangmarketcms/raw/master/shell/file.sh -O file.sh
chmod -R 777 file.sh
source ./file.sh
echo "加载资源文件下载路径完毕"

##### JDK、Tomcat、wangmarket 、wangmarket插件等安装开始####
wget https://gitee.com/HuaweiCloudDeveloper/huaweicloud-solution-build-wangmarketcms/raw/master/shell/tomcat.sh -O tomcat.sh
chmod -R 777 file.sh
source ./tomcat.sh
echo "加载tomcat.sh完毕"
##### JDK、Tomcat安装结束####


#### 开始wangmarket及相关插件安装 ####
wget https://gitee.com/HuaweiCloudDeveloper/huaweicloud-solution-build-wangmarketcms/raw/master/shell/wangmarket.sh -O wangmarket.sh
chmod -R 777 wangmarket.sh
source ./wangmarket.sh
echo "wangmarket及相关插件安装完毕"
#########end###########


#### Mysql 安装后输出的root的登录密码，自己安装就用这种随机生成，要是资源编排安装那应该传入到~/install.properties 中，在去获取出来 ####
mysql_pwd=`java -cp ~/properties.jar Properties -path=/root/install.properties -get mysql.password`
# 数据库名称
db_name=wangmarket
#############
# 安装mysql -- 这里了使用了RDS mysql ，安装的目的是使用mysql远程控制rds数据库进行一些操作
wget https://gitee.com/HuaweiCloudDeveloper/huaweicloud-solution-build-wangmarketcms/raw/master/shell/mysql.sh -O mysql.sh
chmod -R 777 file.sh
source ./mysql.sh
echo "加载mysql.sh完毕"
# mysql rds 的链接ip
install_mysql_ip=`java -cp ~/properties.jar Properties -path=/root/install.properties -get mysql.ip.inside`


############# 导入sql文件开始 #################
# Mysql安装的一些变量设置等。wangmarket数据库是特定的，只有mysql安装在本身服务器才有的
# 创建 wangmarket 数据库
mysql -h $install_mysql_ip -uroot -p$mysql_pwd -e "create database IF NOT EXISTS ${db_name}"
# 导入sql
cd /root
wget $wangmarketSqlDownUrl -O wangmarket.sql
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "source /root/wangmarket.sql"
sleep 3
############# 导入sql文件结束 #################

################ 注意，下面这些是当前标准版版特有的 ##################
#
# 自动修改wangmarket中的system数据表中的数据
#
# 配置安装域名相关-泛解析域名
install_domain=`java -cp ~/properties.jar Properties -path=/root/install.properties -get domain`
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='$install_domain' WHERE name = 'AUTO_ASSIGN_DOMAIN'"
# 配置后台访问域名
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='http://admin.$install_domain/' WHERE name = 'MASTER_SITE_URL'"
# 配置安装域名相关-cdn附件域名
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='//cdn.$install_domain/' WHERE name = 'ATTACHMENT_FILE_URL'"
# 配置存储方式为华为云OBS存储
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='huaWeiYunOBS' WHERE name = 'ATTACHMENT_FILE_MODE'"
##### 配置华为云的一些参数 #####
# 配置华为云AK
# 下载DSA加密支持
cd ~
wget http://down.zvo.cn/wm/version/wm-2.20.jar -O wm.jar
wget http://down.zvo.cn/jar/xnx3-util-1.9.jar -O xnx3-util.jar
######## 获取华为云AK进行加密后加入配置 #######
install_accesskeyid_original=`java -cp ~/properties.jar Properties -path=/root/install.properties -get accesskey.id`
install_accesskeysecret_original=`java -cp ~/properties.jar Properties -path=/root/install.properties -get accesskey.secret`
# 加密
install_accesskeyid=`java -cp ./wm.jar:./xnx3-util.jar  com.xnx3.j2ee.util.DSAUtil ${install_accesskeyid_original}`
install_accesskeysecret=`java -cp ./wm.jar:./xnx3-util.jar  com.xnx3.j2ee.util.DSAUtil ${install_accesskeysecret_original}`
# 将AK保存到配置中
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='$install_accesskeyid' WHERE name = 'HUAWEIYUN_ACCESSKEYID'"
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "INSERT INTO system(name,value) VALUES('HUAWEIYUN_ACCESSKEYID', '$install_accesskeyid')"
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='$install_accesskeysecret' WHERE name = 'HUAWEIYUN_ACCESSKEYSECRET'"
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "INSERT INTO system(name,value) VALUES('HUAWEIYUN_ACCESSKEYSECRET', '$install_accesskeysecret')"
# 配置华为云OBS - bucketname
install_obsbucket=`java -cp ~/properties.jar Properties -path=/root/install.properties -get obs.bucket`
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='$install_obsbucket' WHERE name = 'HUAWEIYUN_OBS_BUCKETNAME'"
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "INSERT INTO system(name,value) VALUES('HUAWEIYUN_OBS_BUCKETNAME', '$install_obsbucket')"
# 配置华为云可用区，如 ap-southeast-1
install_region=`java -cp ~/properties.jar Properties -path=/root/install.properties -get region`
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='$install_region' WHERE name = 'HUAWEIYUN_COMMON_ENDPOINT'"
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "INSERT INTO system(name,value) VALUES('HUAWEIYUN_COMMON_ENDPOINT', '$install_region')"
# 配置华为云OBS - endpoint ，格式如 obs.ap-southeast-1.myhuaweicloud.com
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='obs.$install_region.myhuaweicloud.com' WHERE name = 'HUAWEIYUN_OBS_ENDPOINT'"
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "INSERT INTO system(name,value) VALUES('HUAWEIYUN_OBS_ENDPOINT', 'obs.$install_region.myhuaweicloud.com')"
# 禁止通过 /install/index.do 在安装系统。 注意，这个在入门版本是没有的，入门版本是有安装入口，需要填写域名的
mysql -h $install_mysql_ip -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='false' WHERE name = 'IW_AUTO_INSTALL_USE'"

#
# 修改网市场配置文件中的一些配置
#
# 连接的账密
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties -set database.name=wangmarket
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties -set database.ip=$install_mysql_ip
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties -set spring.datasource.username=root
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties -set spring.datasource.password=$mysql_pwd
# 修改数据库连接为mysql方式，而非默认的sqlite
sed -i "s#^spring.jpa.database-platform=.*# #g" /mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties
sed -i "s#^spring.datasource.driver-class-name=.*#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver#g" /mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties
sed -i "s#^spring.datasource.url=.*#spring.datasource.url=jdbc:mysql://$install_mysql_ip:3306/wangmarket\?useUnicode=true\&characterEncoding=utf-8#g" /mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties
# 在安装系统时，不显示选择存储位置的设置。其实这个根本就不会出现安装界面，直接执行完就安装好了
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties -set wangmarket.install.selectStorage=false

# 输出mysql配置信息到/root/account.txt
ecsip=`java -cp ~/properties.jar Properties -path=/root/install.properties -get ecs.ip`
ecspassword=`java -cp ~/properties.jar Properties -path=/root/install.properties -get ecs.password`
install_cdn_cname=`java -cp ~/properties.jar Properties -path=/root/install.properties -get cdn.cname`
# 配置将文件地址
txt_path=/root/account.txt
touch $txt_path
echo '======= mysql5.7 ======' >> $txt_path
echo '数据库名称:'$db_name >> $txt_path
echo '登录用户名:root' >> $txt_path
echo '登录的密码:'$mysql_pwd >> $txt_path
echo ' ' >> $txt_path
echo '======= 服务器 ======' >> $txt_path
echo '服务器IP:'$ecsip >> $txt_path
echo '登录用户名:root' >> $txt_path
echo '登录的密码:'$ecspassword >> $txt_path
echo ' ' >> $txt_path
echo '======= 对象存储 OBS ======' >> $txt_path
echo '可用区:'$install_region >> $txt_path
echo '桶名字:'$install_obsbucket >> $txt_path
echo ' ' >> $txt_path
echo '======= CDN ======' >> $txt_path
echo '加速域名:cdn.'$install_domain >> $txt_path
echo 'CNAME解析地址:'$install_cdn_cname >> $txt_path


# 删除 file.sh 、mysql.sh 等通用模块
rm -rf /root/file.sh
rm -rf /root/mysql.sh
rm -rf /root/tomcat.sh
rm -rf /root/wangmarket.sh

systemctl stop firewalld.service
# 启动tomcat
echo "启动tomcat"
/mnt/tomcat8/bin/startup.sh
# 关闭mysql
systemctl stop mysqld
# 关闭mysql的开机自启动
systemctl disable mysqld
#看启动日志
#echo "看启动日志"
echo "自动部署完成，您可以正常使用了！打开浏览器，访问ip即可使用"